package com.lb.uts.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lb.uts.util.SpringContextUtil;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * 增删改查统计的默认实现
 *
 * @author liangb
 * @date 2019/3/27 11:44
 */
public abstract class AbstractBaseService<T extends BaseMapper<K>, K extends Serializable> implements IBaseService<T, K> {

    private Class<T> getTClass() {
        Class<T> tClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
        return tClass;
    }

    @Override
    public int insert(K k) {
        return SpringContextUtil.getBean(getTClass()).insert(k);
    }

    @Override
    public int update(K k) {
        return SpringContextUtil.getBean(getTClass()).updateById(k);
    }

    @Override
    public int deleteById(String id) {
        return SpringContextUtil.getBean(getTClass()).deleteById(id);
    }

    @Override
    public K getById(String id) {
        return SpringContextUtil.getBean(getTClass()).selectById(id);
    }

    @Override
    public List<K> queryByEntity(K k) {
        return SpringContextUtil.getBean(getTClass()).selectList(new QueryWrapper(k));
    }

    @Override
    public int countByEntity(K k) {
        return SpringContextUtil.getBean(getTClass()).selectCount(new QueryWrapper(k));
    }
}
